/*   1:    */package com.java.diagnostics.visualizer.recommender.nativeMemory;
/*   2:    */
/*   3:    */import com.java.diagnostics.visualizer.data.AggregateData;
/*   4:    */import com.java.diagnostics.visualizer.data.DataPoint;
/*   5:    */import com.java.diagnostics.visualizer.data.TupleData;
/*   6:    */import com.java.diagnostics.visualizer.recommender.Recommendation;
/*   7:    */import com.java.diagnostics.visualizer.recommender.RecommendationBase;
/*   8:    */import com.java.diagnostics.visualizer.recommender.util.Messages;
/*   9:    */import java.text.MessageFormat;
/*  10:    */
/*  21:    */public class LookForLeaks
/*  22:    */  extends RecommendationBase
/*  23:    */  implements Recommendation
/*  24:    */{
/*  25: 25 */  private static final String NO_LEAK_COMMENT = Messages.getString("no.native.leaks");
/*  26:    */  
/*  27:    */  private static final int ONE_HUNDRED_PERCENT = 100;
/*  28: 28 */  private static final String USAGE_GROWING = Messages.getString("native.usage.growing");
/*  29:    */  
/*  30: 30 */  private static final String LEAK_WARNING = Messages.getString("native.leak.warning");
/*  31:    */  
/*  35:    */  public void recommend(AggregateData data)
/*  36:    */  {
/*  37: 37 */    TupleData reservedSpace = data
/*  38: 38 */      .getTupleData("NativeMemoryLabels.reserved");
/*  39:    */    
/*  40: 40 */    TupleData inuseSpace = data
/*  41: 41 */      .getTupleData("NativeMemoryLabels.inuse");
/*  42:    */    
/*  47: 47 */    if ((reservedSpace != null) && (!reservedSpace.isEmpty())) {
/*  48: 48 */      int percentHeapIncrease = calculateIncrease(reservedSpace);
/*  49:    */      
/*  50: 50 */      int leakThreshold = 15;
/*  51:    */      
/*  52: 52 */      boolean heapIncreasing = percentHeapIncrease > leakThreshold;
/*  53: 53 */      if (heapIncreasing) {
/*  54: 54 */        if ((inuseSpace != null) && (!inuseSpace.isEmpty())) {
/*  55: 55 */          int percentUsedIncrease = calculateIncrease(inuseSpace);
/*  56:    */          
/*  57: 57 */          double inUseThreshold = percentHeapIncrease / 1.5D;
/*  58:    */          
/*  59: 59 */          if (inUseThreshold < percentUsedIncrease)
/*  60:    */          {
/*  61: 61 */            String usageComment = MessageFormat.format(
/*  62: 62 */              USAGE_GROWING, new Object[] {
/*  63: 63 */              Integer.valueOf(percentHeapIncrease), 
/*  64: 64 */              Integer.valueOf(percentUsedIncrease) });
/*  65: 65 */            addWarningToNativeAnalysis(data, usageComment);
/*  66:    */          }
/*  67:    */          else {
/*  68: 68 */            String usageComment = MessageFormat.format(
/*  69: 69 */              LEAK_WARNING, new Object[] {
/*  70: 70 */              Integer.valueOf(percentHeapIncrease), 
/*  71: 71 */              Integer.valueOf(percentUsedIncrease) });
/*  72: 72 */            addProblemToNativeAnalysis(data, usageComment);
/*  73:    */          }
/*  74:    */        }
/*  75:    */      } else {
/*  76: 76 */        addGoodThingToNativeAnalysis(data, NO_LEAK_COMMENT);
/*  77:    */      }
/*  78:    */    }
/*  79:    */  }
/*  80:    */  
/*  81:    */  private int calculateIncrease(TupleData tuple) {
/*  82: 82 */    int numBuckets = 3;
/*  83:    */    
/*  84: 84 */    double earlyMemory = 0.0D;
/*  85: 85 */    double lateMemory = 0.0D;
/*  86:    */    
/*  87: 87 */    DataPoint[] dataPoints = tuple.getDataPoints();
/*  88: 88 */    int spacing = dataPoints.length / numBuckets;
/*  89:    */    
/*  90: 90 */    for (int i = spacing; i < 2 * spacing; i++) {
/*  91: 91 */      DataPoint point = dataPoints[i];
/*  92: 92 */      double usedHeap = point.getRawY();
/*  93: 93 */      earlyMemory += usedHeap;
/*  94:    */    }
/*  95:    */    
/*  96: 96 */    for (int i = dataPoints.length - spacing; i < dataPoints.length; i++) {
/*  97: 97 */      DataPoint point = dataPoints[i];
/*  98: 98 */      double usedHeap = point.getRawY();
/*  99: 99 */      lateMemory += usedHeap;
/* 100:    */    }
/* 101:    */    
/* 102:102 */    int percentHeapIncrease = (int)(lateMemory / earlyMemory * 
/* 103:103 */      100.0D - 100.0D);
/* 104:    */    
/* 105:105 */    return percentHeapIncrease;
/* 106:    */  }
/* 107:    */}


